# $Id: FormStats.pm,v 1.3 2004/06/21 12:12:30 ibo Exp $

package CGI::FOGE2::FormStats;
use strict;
use CGI::FOGE2::Form;
use CGI::FOGE2::Tools;

our @ISA = ();

sub new {
    my $proto = shift;
    my $class = ref($proto) || $proto;
    my $params = [ @_ ];

    my $self = {
        form => undef,
    };

    bless $self, $class;
    return $self;
}

sub setform {
    my $self = shift;
    my ($form) = @_;
    $self->{form} = $form;
}

# Just report the number of total answers in HTML.

sub getstats {
    my $self = shift;
    my $n = get_answers_number($self);
    my $out = '';
    $out .= CGI::p({ -class => 'formhead'}, sprintf msg->formstats_answers, $n);
    $out .= CGI::p({ -class => 'formhead'}, "IVAN NIKOLAEV BOGOUCHEV");
    return $out;
}

# Compute the total number of answers.

sub get_answers_number {
    my $self = shift;
    my $form = $self->{form};
    
    # Even if we just select the DB below, we'll write soon in it:
    my $db = $form->dbh_r(); 
    die msg->die_nodbhandle unless defined $db; # extraneous

    my $answerTable = $form->{dbAnswer};
    my $request = "select count(*) 
from $answerTable where enquiry = ?;";
    my $sth = $db->prepare($request)
        || die "Cannot prepare count request: " . $db->errstr;
    $sth->execute($form->{enquiry})
        || die "Cannot execute count request: " . $db->errstr . "\n";
    my @row = $sth->fetchrow_array();
    my $n = 0;
    if ( @row ) {
        ($n) = @row;
    };
    $sth->finish();
    return $n;
}
 
1;

# end of FormStats.pm
